package com.zr.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.zr.dao.FunctionDao;
import com.zr.model.Function;
import com.zr.util.JDBCUtil;

/**
 * @Description
 * @author 郑少杰
 * @date 2017年9月14日 下午2:56:35
 * @version V1.3.1
 */

public class FunctionDaoImpl implements FunctionDao {

    /**
     * Description
     * 
     * @return
     * @see com.zr.dao.FunctionDao#selectAllFuncs()
     */

    public List<Function> selectAllFuncs() {
        List<Function> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer();
        sql.append("select * from function");
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql.toString());
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Function function = new Function(resultSet.getInt("fid"),
                        resultSet.getString("fname"), resultSet.getString("furl"),
                        resultSet.getInt("fpid"), resultSet.getString("fstate"));
                list.add(function);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JDBCUtil.closeJDBC(preparedStatement, connection);
        }

        return list;
    }

    /**
     * Description
     * 
     * @param page
     * @param pageSize
     * @return
     * @see com.zr.dao.FunctionDao#selAllFuncs(int, int)
     */

    @Override
    public List<Function> selAllFuncs(int page, int pageSize) {
        List<Function> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer();
        sql.append("select * from function ");
        sql.append("limit ?,?");
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql.toString());
            preparedStatement.setInt(1, (page - 1) * pageSize);
            preparedStatement.setInt(2, pageSize);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Function function = new Function(resultSet.getInt("fid"),
                        resultSet.getString("fname"), resultSet.getString("furl"),
                        resultSet.getInt("fpid"), resultSet.getString("fstate"));
                list.add(function);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JDBCUtil.closeJDBC(preparedStatement, connection);
        }

        return list;
    }

    /**
     * Description
     * 
     * @return
     * @see com.zr.dao.FunctionDao#getParentNodeFunById()
     */

    @Override
    public JSONArray getParentNodeFunById(int parentId) {
        JSONArray jsonArray = new JSONArray();
        StringBuffer sql = new StringBuffer();
        sql.append("select * from function where ");
        sql.append("fpid = ?");
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql.toString());
            preparedStatement.setInt(1, parentId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", resultSet.getInt("fid"));
                jsonObject.put("text", resultSet.getString("fname"));
                jsonObject.put("state", resultSet.getString("fstate"));
                JSONObject attr = new JSONObject();
                attr.put("furl", resultSet.getString("furl"));
                jsonObject.put("attributes", attr);

                jsonArray.put(jsonObject);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JDBCUtil.closeJDBC(preparedStatement, connection);
        }

        return jsonArray;
    }

    /**
     * Description
     * 
     * @param parentId
     * @return
     * @see com.zr.dao.FunctionDao#getAllParentNodes(int)
     */

    @Override
    public JSONArray getAllParentNodes(int parentId) {
        JSONArray jsonArray = getParentNodeFunById(parentId);

        for (int i = 0; i < jsonArray.length(); i++) {
            String state = jsonArray.getJSONObject(i).getString("state");
            if ("open".equals(state)) {
                jsonArray.remove(i);
                i--;
            } else {
                int currentId = jsonArray.getJSONObject(i).getInt("id");
                JSONArray children = this.getAllParentNodes(currentId);
                jsonArray.getJSONObject(i).put("children", children);
            }
        }
        return jsonArray;
    }

    /**
     * Description
     * 
     * @param name
     * @param state
     * @param fpid
     * @return
     * @see com.zr.dao.FunctionDao#insertFuncs(java.lang.String, java.lang.String, int)
     */

    @Override
    public boolean insertFuncs(String name, String state, int fpid) {
        StringBuffer sql = new StringBuffer();
        sql.append("insert into function (fname,fpid,fstate) ");
        sql.append("values(?,?,?)");
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql.toString());
            preparedStatement.setString(1, name);
            preparedStatement.setInt(2, fpid);
            preparedStatement.setString(3, state);
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                return true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JDBCUtil.closeJDBC(preparedStatement, connection);
        }
        return false;
    }

}
